﻿<UserControl x:Class="FR.SAFTAnalyserLite.Views.InvoicesSummaryView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:conv="clr-namespace:FR.SAFTAnalyserLite.ValueConverters"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <conv:GroupTotalSummaryConverter  x:Key="myConverter" />
        <CollectionViewSource x:Key="invoices" Source="{Binding Invoices, IsAsync=True}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Tipo"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        <CollectionViewSource x:Key="invoiceLines" Source="{Binding InvoiceLines, IsAsync=True}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Tipo"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        <CollectionViewSource x:Key="taxes" Source="{Binding Resumo, IsAsync=True}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Codigo"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </UserControl.Resources>

    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <Grid ScrollViewer.CanContentScroll="True" >
            <Grid.RowDefinitions>
                <RowDefinition Height="70"/>
                <RowDefinition Height="250"/>
                <RowDefinition Height="250"/>
                <RowDefinition Height="250"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50*"/>
                <ColumnDefinition Width="50*"/>
            </Grid.ColumnDefinitions>

            <!-- Total dos documentos -->
            <GroupBox Header="Totais" Grid.Row="0" Grid.Column="0">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Total débitos:" Margin="3"/>
                    <TextBlock Text="{Binding TotalDebit, StringFormat=\{0:C\}}" Margin="3"/>
                    <TextBlock Text="Total créditos:" Margin="3"/>
                    <TextBlock Text="{Binding TotalCredit, StringFormat=\{0:C\}}" Margin="3"/>
                </StackPanel>
            </GroupBox>

            <!-- exportar excel -->
            <GroupBox Header="Exportar" Grid.Row="0" Grid.Column="1">
                <StackPanel Orientation="Horizontal">
                    <Button Width="150" Height="25" Margin="5" Padding="3" ToolTip="Exportar para Excel" DockPanel.Dock="Bottom" 
                        Command="{Binding DoExportDocumentsCommand}">
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{StaticResource Excel}"/>
                            <TextBlock Text="Documentos" Padding="5,0,0,0"/>
                        </StackPanel>
                    </Button>
                    <Button Width="150" Height="25" Margin="5" Padding="3" ToolTip="Exportar para Excel" DockPanel.Dock="Bottom" 
                        Command="{Binding DoExportlinesCommand}">
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{StaticResource Excel}"/>
                            <TextBlock Text="Linhas" Padding="5,0,0,0"/>
                        </StackPanel>
                    </Button>
                    <Button DockPanel.Dock="Top" Width="150" Height="25" Margin="5" Padding="3" ToolTip="Exportar para Excel"
                        Command="{Binding DoExportTaxesCommand}">
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{StaticResource Excel}"/>
                            <TextBlock Text="Impostos" Padding="5,0,0,0"/>
                        </StackPanel>
                    </Button>
                </StackPanel>
            </GroupBox>

            <!-- Total dos documentos -->
            <DockPanel Grid.Row="1" Grid.ColumnSpan="2">
                <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
                    <RadioButton Content="Documentos por mês" GroupName="documentos" IsChecked="{Binding AgruparDocumentosMes}" Margin="3" FontSize="12"/>
                    <RadioButton Content="Documentos por dia" GroupName="documentos" IsChecked="{Binding AgruparDocumentosDia}" Margin="3" FontSize="12"/>
                </StackPanel>

                <DataGrid x:Name="dataGridInvoices" TabIndex="1" DockPanel.Dock="Bottom"
                      ItemsSource="{Binding Source={StaticResource invoices}}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Tipo" Binding="{Binding Tipo}" />
                        <DataGridTextColumn Header="Data" Binding="{Binding Data, StringFormat=\{0:d\}}" />
                        <DataGridTextColumn Header="Total s/ IVA" Binding="{Binding Incidencia, StringFormat=\{0:N\}}" Width="100"/>
                        <DataGridTextColumn Header="Total" Binding="{Binding Total, StringFormat=\{0:N\}}" Width="70"/>
                        <DataGridTextColumn Header="Total imposto" Binding="{Binding Imposto}" Width="100"/>
                    </DataGrid.Columns>

                    <DataGrid.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <TextBlock Text="{Binding Path=Name}" />
                                    </StackPanel>
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                            <GroupStyle.ContainerStyle>
                                <Style TargetType="{x:Type GroupItem}">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                                <Expander IsExpanded="False">
                                                    <Expander.Header>
                                                        <StackPanel Orientation="Horizontal">
                                                            <TextBlock Text="{Binding Path=Name}"/>
                                                            <TextBlock Text=" ( " />
                                                            <TextBlock Text="{Binding Path=ItemCount}" />
                                                            <TextBlock Text=" ) " />
                                                            <TextBlock Text="{Binding Path=Items, Converter={StaticResource myConverter}}" HorizontalAlignment="Right"/>
                                                        </StackPanel>
                                                    </Expander.Header>
                                                    <ItemsPresenter />
                                                </Expander>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupStyle.ContainerStyle>
                        </GroupStyle>
                    </DataGrid.GroupStyle>

                </DataGrid>
            </DockPanel>

            <!-- Total das linhas -->
            <DockPanel Grid.Row="2" Grid.ColumnSpan="2">
                <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
                    <RadioButton Content="Linhas por código" GroupName="linhas" IsChecked="{Binding AgruparLinhasPorCodigo}" Margin="3" FontSize="12"/>
                    <RadioButton Content="Linhas por descrição" GroupName="linhas" IsChecked="{Binding AgruparLinhasPorDescricao}" Margin="3" FontSize="12"/>
                </StackPanel>

                <DataGrid x:Name="dataGridInvoiceLines" TabIndex="1" DockPanel.Dock="Bottom"
                      ItemsSource="{Binding Source={StaticResource invoiceLines}}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Tipo" Binding="{Binding Tipo}" />
                        <DataGridTextColumn Header="Produto" Binding="{Binding Produtos}" />
                        <DataGridTextColumn Header="Total s/ IVA" Binding="{Binding Incidencia, StringFormat=\{0:N\}}" Width="90"/>
                        <DataGridTextColumn Header="Imposto" Binding="{Binding TaxaImposto}" />
                    </DataGrid.Columns>

                    <DataGrid.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <TextBlock Text="{Binding Path=Name}" />
                                    </StackPanel>
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                            <GroupStyle.ContainerStyle>
                                <Style TargetType="{x:Type GroupItem}">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                                <Expander IsExpanded="False">
                                                    <Expander.Header>
                                                        <StackPanel Orientation="Horizontal">
                                                            <TextBlock Text="{Binding Path=Name}" />
                                                            <TextBlock Text=" ( " />
                                                            <TextBlock Text="{Binding Path=ItemCount}" />
                                                            <TextBlock Text=" ) " />
                                                            <TextBlock Text="{Binding Path=Items, Converter={StaticResource myConverter}}" HorizontalAlignment="Right"/>
                                                        </StackPanel>
                                                    </Expander.Header>
                                                    <ItemsPresenter />
                                                </Expander>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupStyle.ContainerStyle>
                        </GroupStyle>
                    </DataGrid.GroupStyle>

                </DataGrid>
            </DockPanel>

            <!-- Totais dos impostos -->
            <DockPanel Grid.Row="3" Grid.ColumnSpan="2">
                <DataGrid x:Name="dataGridTaxes" TabIndex="1" DockPanel.Dock="Bottom"
                      ItemsSource="{Binding Source={StaticResource taxes}}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Taxa" Binding="{Binding Taxa}" />
                        <DataGridTextColumn Header="Codigo" Binding="{Binding Codigo}" />
                        <DataGridTextColumn Header="Incidencia" Binding="{Binding Incidencia, StringFormat=\{0:N\}}" Width="90"/>
                        <DataGridTextColumn Header="Valor" Binding="{Binding Valor, StringFormat=\{0:N\}}" Width="90"/>
                    </DataGrid.Columns>

                    <DataGrid.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <TextBlock Text="{Binding Path=Name}" />
                                    </StackPanel>
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                            <GroupStyle.ContainerStyle>
                                <Style TargetType="{x:Type GroupItem}">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                                <Expander IsExpanded="False">
                                                    <Expander.Header>
                                                        <StackPanel Orientation="Horizontal">
                                                            <TextBlock Text="{Binding Path=Name}" />
                                                            <TextBlock Text=" ( " />
                                                            <TextBlock Text="{Binding Path=ItemCount}" />
                                                            <TextBlock Text=" ) " />
                                                            <TextBlock Text="{Binding Path=Items, Converter={StaticResource myConverter}}" HorizontalAlignment="Right"/>
                                                        </StackPanel>
                                                    </Expander.Header>
                                                    <ItemsPresenter />
                                                </Expander>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupStyle.ContainerStyle>
                        </GroupStyle>
                    </DataGrid.GroupStyle>

                </DataGrid>
            </DockPanel>
        </Grid>
    </ScrollViewer>
</UserControl>
